// Code licensed under the MIT license - see the license file for details
// Copyright 2007 Bryan Smith

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class mainClass extends JFrame implements ActionListener {

	public JTextArea main_text;
	public JTextField file_text;
	static final long serialVersionUID = 1;
	public mainClass() {
		setSize(700,500);
		setTitle("Branbesmi");

		Container con = getContentPane();
		con.setLayout(new BorderLayout());

		JMenuBar menu_bar = new JMenuBar();
		JMenu main_menu = new JMenu("Menu");
		JMenuItem item;

		item = new JMenuItem("Choose File");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
		item.addActionListener(this);
		main_menu.add(item);

		// http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
		item = new JMenuItem("New");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		item.addActionListener(this);
		main_menu.add(item);

		item = new JMenuItem("Open");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		item.addActionListener(this);
		main_menu.add(item);

		item = new JMenuItem("Save");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		item.addActionListener(this);
		main_menu.add(item);

		item = new JMenuItem("About");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));
		item.addActionListener(this);
		main_menu.add(item);

		item = new JMenuItem("Exit");
		item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
		item.addActionListener(this);
		main_menu.add(item);

		menu_bar.add(main_menu);
		setJMenuBar(menu_bar);

		main_text = new JTextArea();
		main_text.setLineWrap(true);
		main_text.setWrapStyleWord(true);
		con.add(main_text, BorderLayout.CENTER);
		JScrollPane scroll_text = new JScrollPane(main_text);
		scroll_text.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		con.add(scroll_text, BorderLayout.CENTER);

		file_text = new JTextField();
		con.add(file_text, BorderLayout.SOUTH);

		windowDestroy listen = new windowDestroy();
		addWindowListener(listen);
	}

	public void chooseFile() {
		// http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
		JFileChooser chFile = new JFileChooser();
		int dialog = chFile.showOpenDialog(this);
		if (dialog == JFileChooser.APPROVE_OPTION) {
			File name = chFile.getSelectedFile();
			file_text.setText(name.getAbsolutePath()); // http://java.sun.com/javase/6/docs/api/ (File class reference)
		}
	}

	public void newFile() {
		main_text.setText("");
		file_text.setText("");
	}

	public void openFile() {
		String file_name = file_text.getText();
		String curLine;
		String theText = "";
		BufferedReader in_file = null;
		try {
			in_file = new BufferedReader(new FileReader(file_name));
			while (true) {
				curLine = in_file.readLine();
				if (curLine == null) {
					break;
				} else {
					theText = theText + curLine +  "\n";
				}
			}
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "IO Error - Possibly the file doesn't exist.");
		}
		main_text.setText(theText);
	}

	public void saveFile() {
		String file_name = file_text.getText();
		PrintWriter out_file = null;
		try {
			out_file = new PrintWriter(new FileOutputStream(file_name));
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "IO Error - Possibly insufficient write privileges.");
		}
		out_file.print(main_text.getText());
		out_file.close();
	}

	public void about() {
		JOptionPane.showMessageDialog(null, "Created by Bryan Smith. (c) 2007.\nVersion: Build 104 (svn).");
	}

	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals("Choose File"))
			chooseFile();
		else if (command.equals("New"))
			newFile();
		else if (command.equals("Open"))
			openFile();
		else if (command.equals("Save"))
			saveFile();
		else if (command.equals("About"))
			about();
		else if (command.equals("Exit"))
			System.exit(0);
		else
			System.out.println("Error!!");
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			System.out.println("Unsupported Look and Feel");
		} catch (IllegalAccessException e) {
			System.out.println("Illegal Access");
		} catch (ClassNotFoundException e) {
			System.out.println("Class Not Found");
		} catch (InstantiationException e) {
			System.out.println("Instantiation Problem");
		} // http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html
		mainClass bran = new mainClass();
		bran.setVisible(true);
	}
}
